// SmartInput Chrome扩展统一配置
class SmartInputConfig {
  constructor() {
    // 默认配置
    this.defaults = {
      // API配置
      // apiBase: 'https://192.168.137.122/api',
      // wsBase: 'wss://192.168.137.122/ws',
      apiBase: 'https://api.mensanyun.com/api',
      wsBase: 'wss://api.mensanyun.com/ws',
      
      // 设备配置
      deviceName: 'Chrome Extension',
      deviceType: 'browser_extension',
      
      // 功能开关
      autoConnect: true,
      voiceEnabled: true,
      syncEnabled: true,
      autoInsert: true,
      syncNotification: true,
      showFloatingPanel: false,
      showVoiceButton: true,
      
      // 语音配置
      voiceLanguage: 'zh-CN',
      voiceConfidence: 0.7,
      
      // 同步配置
      syncTarget: 'all',
      
      // 界面配置
      theme: 'light',
      panelPosition: 'top-right',
      
      // 快捷键配置
      voiceShortcut: 'Ctrl+Shift+V',
      syncShortcut: 'Ctrl+Shift+S',
      
      // 调试配置
      debugMode: false,
      logLevel: 'info'
    };
    
    // 当前配置
    this.config = { ...this.defaults };
  }
  
  // 获取配置值
  get(key) {
    return this.config[key] || this.defaults[key];
  }
  
  // 设置配置值
  set(key, value) {
    this.config[key] = value;
  }
  
  // 获取API基础URL
  getApiBase() {
    return this.get('apiBase');
  }
  
  // 获取WebSocket基础URL
  getWsBase() {
    return this.get('wsBase');
  }
  
  // 构建完整的API URL
  buildApiUrl(endpoint) {
    const apiBase = this.getApiBase();
    return `${apiBase}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
  }
  
  // 构建WebSocket URL
  buildWsUrl(deviceId, token) {
    const wsBase = this.getWsBase();
    const params = new URLSearchParams({
      token: token,
      deviceId: deviceId
    });
    return `${wsBase}/${deviceId}?${params.toString()}`;
  }
  
  // 从存储加载配置
  async loadFromStorage() {
    try {
      const stored = await chrome.storage.local.get(Object.keys(this.defaults));
      
      // 合并存储的配置和默认配置
      this.config = { ...this.defaults, ...stored };
      
      console.log('配置已从存储加载:', this.config);
      return this.config;
    } catch (error) {
      console.error('加载配置失败:', error);
      return this.config;
    }
  }
  
  // 保存配置到存储
  async saveToStorage() {
    try {
      await chrome.storage.local.set(this.config);
      console.log('配置已保存到存储:', this.config);
      return true;
    } catch (error) {
      console.error('保存配置失败:', error);
      return false;
    }
  }
  
  // 重置为默认配置
  async resetToDefaults() {
    this.config = { ...this.defaults };
    await this.saveToStorage();
    console.log('配置已重置为默认值');
  }
  
  // 获取所有配置
  getAll() {
    return { ...this.config };
  }
  
  // 更新配置
  async update(newConfig) {
    this.config = { ...this.config, ...newConfig };
    await this.saveToStorage();
    console.log('配置已更新:', this.config);
  }
  
  // 验证配置
  validate() {
    const errors = [];
    
    // 验证API地址
    try {
      new URL(this.getApiBase());
    } catch (error) {
      errors.push('API地址格式无效');
    }
    
    // 验证WebSocket地址
    try {
      new URL(this.getWsBase());
    } catch (error) {
      errors.push('WebSocket地址格式无效');
    }
    
    // 验证设备名称
    if (!this.get('deviceName') || this.get('deviceName').trim() === '') {
      errors.push('设备名称不能为空');
    }
    
    return {
      valid: errors.length === 0,
      errors: errors
    };
  }
  
  // 获取配置摘要
  getSummary() {
    return {
      apiBase: this.getApiBase(),
      wsBase: this.getWsBase(),
      deviceName: this.get('deviceName'),
      autoConnect: this.get('autoConnect'),
      voiceEnabled: this.get('voiceEnabled'),
      syncEnabled: this.get('syncEnabled'),
      theme: this.get('theme')
    };
  }
}

// 创建全局配置实例
const smartInputConfig = new SmartInputConfig();

// 导出配置实例
if (typeof module !== 'undefined' && module.exports) {
  module.exports = smartInputConfig;
} else {
  // 在浏览器环境中，将配置实例挂载到全局对象
  window.smartInputConfig = smartInputConfig;
} 